package br.com.fatecpg.locacaobicicletas;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class ConsultaBicicleta extends javax.swing.JInternalFrame {

    public int novo;

    public ConsultaBicicleta() {
        initComponents();
    }

    private void atualiza() throws SQLException {
        DefaultTableModel model = (DefaultTableModel) bikejTable.getModel();
        model.setRowCount(0);
        ResultSet rs = ConexãoBike.executar("SELECT *FROM BICICLETA");
        if (rs != null) {
            while (rs.next()) {
                model.addRow(new Object[]{
                            rs.getInt("cd_bicicleta"),
                            rs.getString("nm_estacao_bicicleta")
                        });
            }
        }
    }

    private void Excluirbike() throws SQLException {
        if (bikejTable.getSelectedRow() >= 0) {
            String item = bikejTable.getValueAt(bikejTable.getSelectedRow(), 0).toString();
            if (JOptionPane.showConfirmDialog(this, "Excluir Bicicleta " + item) == JOptionPane.YES_NO_OPTION) {
                String sql = "delete from bicicleta where cd_bicicleta =" + item;
                try {
                    ConexãoBike.ConexãoJDBC.createStatement().execute(sql);
                    this.atualiza();
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(this, "Erro ao tentar excluir registro" + ex.getMessage(),
                            "Erro", JOptionPane.ERROR_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Registro não Encontrado.",
                    "Aviso", JOptionPane.WARNING_MESSAGE);
        }
    }

    private void alterabike() {
        DefaultTableModel modelo = (DefaultTableModel) bikejTable.getModel();
        int linhas = bikejTable.getRowCount();
        String SQL = "";
        
        for (int i = 0; i < linhas; i++) {
            int cod = Integer.parseInt(bikejTable.getValueAt(i, 0).toString());
            String est = bikejTable.getValueAt(i, 1).toString();
            SQL = "update bicicleta " + "set nm_estacao_bicicleta = '"+ est +"'"
                    + "where cd_bicicleta =" + cod;

            try {
                ConexãoBike.ConexãoJDBC.createStatement().execute(SQL);
                //JOptionPane.showMessageDialog(null, "Alterações Efetuadas com Sucesso");
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Campos Não Alterados. Erro : "
                        + ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
        JOptionPane.showMessageDialog(null, "Alterações Efetuadas com Sucesso");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        bikejTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        consultarjButton = new javax.swing.JButton();
        excluirjButton = new javax.swing.JButton();
        alterajButton = new javax.swing.JButton();
        canceljButton = new javax.swing.JButton();

        setIconifiable(true);
        setMaximizable(true);
        setTitle("Consulta de Bicicletas");

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        bikejTable.setAutoCreateRowSorter(true);
        bikejTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Estação"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(bikejTable);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        consultarjButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        consultarjButton.setText("CONSULTAR");
        consultarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultarjButtonActionPerformed(evt);
            }
        });
        jPanel2.add(consultarjButton);

        excluirjButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        excluirjButton.setText("EXCLUIR");
        excluirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirjButtonActionPerformed(evt);
            }
        });
        jPanel2.add(excluirjButton);

        alterajButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        alterajButton.setText("ALTERAR");
        alterajButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterajButtonActionPerformed(evt);
            }
        });
        jPanel2.add(alterajButton);

        canceljButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        canceljButton.setText("CANCELAR");
        canceljButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                canceljButtonActionPerformed(evt);
            }
        });
        jPanel2.add(canceljButton);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void consultarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultarjButtonActionPerformed
    try {
        atualiza();
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(this, "Erro:" + ex.getMessage(),
                "ERRO", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_consultarjButtonActionPerformed

private void excluirjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirjButtonActionPerformed
    try {
        Excluirbike();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "ERRO" + ex.getMessage(),
                "ERRO", JOptionPane.ERROR_MESSAGE);
    }
    // TODO add your handling code here:
}//GEN-LAST:event_excluirjButtonActionPerformed

private void alterajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterajButtonActionPerformed
    try {
        alterabike();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "ERRO" + ex.getMessage(),
                "ERRO", JOptionPane.ERROR_MESSAGE);
    }

    // TODO add your handling code here:
}//GEN-LAST:event_alterajButtonActionPerformed

private void canceljButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_canceljButtonActionPerformed
    this.setVisible(false);
    // TODO add your handling code here:
}//GEN-LAST:event_canceljButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterajButton;
    private javax.swing.JTable bikejTable;
    private javax.swing.JButton canceljButton;
    private javax.swing.JButton consultarjButton;
    private javax.swing.JButton excluirjButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
